<?php

namespace App\Services\Fund;

use App\Services\Fund\FundAccount\FundAccountLogic;
use App\Services\User\Token;
use App\Services\User\App;
use App\Services\Manage\Api;
use App\Services\User\User;
//use Illuminate\Support\Facades\Request;

/**
 * 用户的资金账户
 * Class FundAccount
 * @package App\Services\Fund
 */
class FundAccount
{

    //封装一层检验user_info、opposite_info后再执行stepRun <该页多数方法都需检验这两个json数据>
    protected function _checkJsonThenRun(array $data, string $method): array
    {
        if(isset($data['user_info']) && $data['user_info'] !== '{}'){ $data['user_info'] = json_encode($data['user_info']); }
        if(isset($data['opposite_info']) && $data['opposite_info'] !== '{}'){ $data['opposite_info'] = json_encode($data['opposite_info']); }
        if(isset($data['total_other_price']) && $data['total_other_price'] !== '{}'){ $data['total_other_price'] = json_encode($data['total_other_price']); }
        if(isset($data['buyer_info']) && $data['buyer_info'] !== '{}'){ $data['buyer_info'] = json_encode($data['buyer_info']); }
        if(isset($data['seller_info']) && $data['seller_info'] !== '{}'){ $data['seller_info'] = json_encode($data['seller_info']); }
        if(isset($data['system_info']) && $data['system_info'] !== '{}'){ $data['system_info'] = json_encode($data['system_info']); }

        return (new FundAccountLogic())->$method($data);
    }

    /*兑换钱*/
    public function exchange($data): array
    {
        $data = Api::addAppId($data);
        ValidateFundAccount::checkValidate($data, ['exchange', 'money', 'app_id', 'currency_scale']);
        $classPath = '\App\Services\Fund\FundAccount' . '\\' . $data['exchange'] . 'AccountLogic';
        $Obj = new $classPath($data);
        return (new FundAccountLogic())->exchange($Obj, $data);
    }

//    // 初始化一个资金账户,如果帐户存在,就清0,如果不存在,就创建一个
//    public function init(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'create_ip'=>' ', 'app_id'=>' ']);
//        return $this->stepRun($data, 'init', 'init');
//    }
    # 充值 系统->a
    public function prepaid($data): array
    {
        $data = Api::addAppId($data);
        ValidateFundAccount::checkValidate($data, ['user_id', 'money', 'update_ip', 'app_id', 'user_info','opposite_info', 'remark']);
        return $this->_checkJsonThenRun($data, 'prepaid');
    }
//    # 转账 a->b
   public function transfer($data): array
   {
         $data = init_array($data,['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}']);
        ValidateFundAccount::checkValidate($data, ['user_id', 'opposite_id', 'money', 'update_ip', 'app_id', 'user_info', 'opposite_info']);
        return (new FundAccountLogic())->transfer($data);

      /* $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}']);
       return $this->_checkJsonThenRun($data, 'transfer', 'transfer');*/
   }
//    # 付款 a->b 暂时废弃,用转账功能完成
//    public function payment(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0']);
//        return $this->stepRun($data, 'payment', 'payment');
//    }
//    # 借款 a借b的
//    public function borrow(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}']);
//        return $this->_checkJsonThenRun($data, 'borrow', 'borrow');
//    }
//    # 还款 a还b的
//    public function refund(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'borrow_log_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}']);
//        return $this->_checkJsonThenRun($data, 'refund', 'refund');
//    }
//    /*唯一索引：`user_id`,`app_id`,`type`,`obj_id`,`freeze_counts`,`freeze_status`*/
//    # 冻结 a
//    public function freeze(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}','type'=>' ','obj_id'=>' ','freeze_counts'=>1]);
//        return $this->_checkJsonThenRun($data, 'freeze', 'freeze');
//    }
    /*管理员冻结某用户资金*/
    public function adminFreeze($data): array
    {
        $data = init_array($data,['user_id'=>0, 'opposite_id'=>0, 'money'=>0, 'update_ip'=>'', 'app_id'=>'', 'user_info'=>'','opposite_info'=>'','reason'=>'','token'=>'']);
        $data = Api::addAppId($data);
        $data['opposite_id'] = SYSTEM_ID;
        $ids = $data['user_id'] . ',' . $data['opposite_id'];
        $user = (new User())->infoByIds(['ids'=>$ids]);
        $userInfos = reset_array_key($user['res'], 'id');
        !empty($userInfos[$data['user_id']]) && $data['user_info'] = $userInfos[$data['user_id']];
        !empty($userInfos[$data['opposite_id']]) && $data['opposite_info'] = $userInfos[$data['opposite_id']];
        $data['update_ip'] = request()->ip();
        ValidateFundAccount::checkValidate($data,['user_id', 'opposite_id', 'money', 'update_ip', 'app_id', 'user_info','opposite_info','reason']);
        return $this->_checkJsonThenRun($data, 'adminFreeze');
    }

    # 解冻 a
    public function unfreeze($data): array
    {
        $data = init_array($data,['user_id'=>0, 'opposite_id'=>0, 'money'=>0, 'update_ip'=>'', 'app_id'=>'', 'user_info'=>'','opposite_info'=>'','type'=>'','obj_id'=>0,'freeze_counts'=>0,'remark'=>'','token'=>'']);
        $token = (new Token())->hGetAll($data['token']);
        $data = Api::addAppId($data);
        $user = (new User())->infoAll(['id'=>$data['user_id']]);
        $data['type'] = 'goods';
        $data['freeze_counts'] = 1;
        $data['opposite_id'] = $user['res']['id'];
        $data['opposite_info'] = $token['res'];
        $data['user_info'] = $user['res'];
        $data['update_ip'] = request()->ip();
        ValidateFundAccount::checkValidate($data,['user_id', 'opposite_id', 'money', 'update_ip', 'app_id', 'user_info','opposite_info','type','obj_id','freeze_counts']);
        return $this->_checkJsonThenRun($data, 'unfreeze');
    }
//    # 红包功能 a->b b(enter)
    // 获取列表
    public function list($data): array
    {
        ValidateFundAccount::checkValidate($data,['page', 'list_rows', 'keywords']);
        return (new FundAccountLogic())->list($data);
    }
    // 某user_ids的用户资金列表
    public function listByUserIds($data): array
    {
        ValidateFundAccount::checkValidate($data, ['user_ids','token']);
        return (new FundAccountLogic())->listByUserIds($data);
    }


    /**
     * 获取某用户的所有资产余额
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function userFund($data): array
    {
        $data = pick_array($data, ['user_id']);

        return (new FundAccountLogic())->userFund($data);
    }
//
//    /*余额付款的订单进行确认收货操作 opposite_id解冻user_id user_id转账opposite_id*/
//    //解冻用户资金 用户转账平台资金
//    public function affirmPickUpGoods(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'certificate_price'=>' ', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}', 'opposite_info'=>'{}', 'type'=>' ', 'order_id'=>' ', 'pay_method'=>' ']);
//        return $this->_checkJsonThenRun($data, 'affirmPickUpGoods');
//    }
//
//    /*我的证书 - 退款*/
//    public function sendBack(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>0, 'opposite_id'=>0, 'order_id'=>0, 'certificate_price'=>' ', 'pay_method'=>' ', 'certi_transfer_fee'=>0, 'earnings'=>0, 'update_ip'=>' ', 'app_id'=>0, 'user_info'=>'{}', 'opposite_info'=>'{}']);
//        return $this->_checkJsonThenRun($data, 'sendBack');
//    }
//
//    // 同意证书转让或购买证书时的资金操作
//    // procedure_cost 手续费
//    // sell_price 证书的销售价格
//    // buyer_id 买家
//    // seller_id 卖家
//    // 验证了抵扣积分信息是否正确
//    public function buyCertificate(): array
//    {
//        $data = $this->haveIssetReceiveData(['certificate_id'=>0, 'buyer_id'=>0, 'seller_id'=>0, 'system_id'=>0, 'sell_price'=>0,'procedure_cost'=>0,'update_ip'=>'','app_id'=>0,'buyer_info'=>'{}','seller_info'=>'{}','system_info'=>'{}','is_use_integral'=>0,'certi_max_integral_deduction'=>0,'integral_deduction'=>0,'integral_deduction_fund'=>0]);
//        return $this->_checkJsonThenRun($data, 'buyCertificate', 'buyCertificate');
//    }
//    // 证书改价时资金的操作
//    public function certiChangePrice(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>0, 'opposite_id'=>0, 'old_procedure_price'=>0, 'new_procedure_price'=>0, 'update_ip'=>'', 'app_id'=>0, 'user_info'=>'{}', 'opposite_info'=>'{}', 'certificate_id'=>0]);
//        return $this->_checkJsonThenRun($data, 'certiChangePrice');
//    }
//    // 解冻用户改价时的手续费(拒绝定向单后，还原证书设置时进行的操作)
//    public function unfreezeProcedure(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>0, 'opposite_id'=>0, 'procedure_price'=>0, 'user_info'=>'{}', 'opposite_info'=>'{}', 'certificate_id'=>0]);
//        return $this->_checkJsonThenRun($data, 'unfreezeProcedure');
//    }

}